﻿// 固定顶部和返回顶部
(function (d, w) {
    $("#select_document").change(function () {
        $("title").text("Swagger UI");
    });

    $("title").text($(".info_title").text() + " - " + document.location.host);

    $("table.parameters").find("tbody.operation-params").children("tr").each(function () {
        var cells = $(this).find("td");

        $(cells[1]).find("input,textarea").attr("placeholder", $.trim($(cells[2]).text()))
    });

    $("input[name='authorization']").each(function () {
        var $current = $(this);

        $(this).after($("<a href='javascript;' style='margin-left: 3px;'>尝试获取!</a>").click(function () {
            $("input[name='authorization']").each(function () {
                var authorization = $(this).val();

                if (authorization != "") {
                    $current.val(authorization);
                }
            });

            return false;
        }));
    });

    $("input[name='page_index']").each(function () {
        var $current = $(this);

        $(this).after($("<a href='javascript;' style='margin-left: 3px;'>填写 1!</a>").click(function () {
            $current.val("1")

            return false;
        }));
    });

    $("input[name='page_size']").each(function () {
        var $current = $(this);

        $(this).after($("<a href='javascript;' style='margin-left: 3px;'>填写 10!</a>").click(function () {
            $current.val("10")

            return false;
        }));
    });

    $("textarea").each(function () {
        var _this = this;

        _this.addEventListener("focus", function () {
            _this.style.height = (_this.scrollHeight - 10) + "px";
            _this.style.overflowY = "hidden";
        });
        _this.addEventListener("blur", function () {
            _this.style.height = null;
            _this.style.overflowY = null;
        });
        _this.addEventListener("input", function () {
            _this.style.height = (_this.scrollHeight - 10) + "px";
        });
    });

    HTMLElement.prototype.hasClass = function (className) {
        return !!this.className.match(new RegExp("(\\s|^)" + className + "(\\s|$)")); // ( \\s|^ ) 判断前面是否有空格 （\\s | $ ）判断后面是否有空格 两个感叹号为转换为布尔值 以方便做判断
    }

    HTMLElement.prototype.addClass = function (className) {
        if (!this.hasClass(className)) {
            this.className = (this.className + " " + className).replace(new RegExp(" {2,}"), " ").trim();
        };
    };

    HTMLElement.prototype.removeClass = function (className) {
        if (this.hasClass(className)) {
            this.className = this.className.replace(new RegExp("(\\s|^)" + className + "(\\s|$)"), " ").replace(new RegExp(" {2,}"), " ").trim(); // replace方法是替换
        };
    };

    // 获取 HTML、BODY 标签
    var html = d.getElementsByTagName("html")[0]
    var body = d.getElementsByTagName("body")[0];

    if (d.getElementById("backtop") == null && d.getElementById("refresh") == null) {
        // 创建 返回顶部 按钮
        var backtop = d.createElement("button");

        backtop.setAttribute("id", "backtop");
        backtop.setAttribute("title", "返回顶部")
        backtop.innerText = "返回顶部";
        backtop.onclick = function () {
            window.scrollTo({
                "behavior": "smooth",
                "top": 0
            });
        };

        body.appendChild(backtop);

        // 创建 返回顶部 按钮
        var refresh = d.createElement("button");

        refresh.setAttribute("id", "refresh");
        refresh.setAttribute("title", "获取最新接口")
        refresh.innerText = "获取最新接口";
        refresh.onclick = function () {
            window.swaggerUi.headerView.showCustom();
        };

        body.appendChild(refresh);
    }

    // 处理页面滚动条
    w.addEventListener('scroll', function () {
        if (w.scrollY > 50) {
            body.addClass("f50");
        }
        else {
            body.removeClass("f50");
        }

        if (w.scrollY > 60) {
            body.addClass("f60");
        }
        else {
            body.removeClass("f60");
        }

        if (w.scrollY > 70) {
            body.addClass("f70");
        }
        else {
            body.removeClass("f70");
        }

        if (w.scrollY > 80) {
            body.addClass("f80");
        }
        else {
            body.removeClass("f80");
        }
    });
})(document, window);
